package production.practice.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import production.practice.pojo.entity.User;
import production.practice.mapper.UserMapper;
import production.practice.service.UserService;
import production.practice.utils.BinaryUtils;
import production.practice.utils.SysUtil;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public String login(String body) {
        JSONObject json = JSONObject.parseObject(body);
        String name = json.getString("userName");
        String password = json.getString("password");
        User user = this.getOne(User.gw().eq(User::getUsername, name).eq(User::getPassword, password));

        if (BinaryUtils.isNotEmpty(user)){
            String token = SysUtil.generateToken(user);
            return token;
        }
        return "";
    }
}
